<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="1500" destroy-on-close
             @closed="$emit('closed')" align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" label-width="130" ref="dialogForm">
            <el-row :gutter="1">
                <el-col :span="6">
                    <el-form-item label="物流单号" prop="logistics_code">
                        <el-input v-model="form.logistics_code" placeholder="请输入物流单号" clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="物流日期" prop="logistics_date">
                        <el-date-picker v-model="form.logistics_date" type="date" placeholder="请选择日期" :value-format="'YYYY-MM-DD'" style="width:100%" />
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="仓库" prop="ware_id">
                        <el-select v-model="form.ware_id" filterable style="width: 100%" placeholder="仓库" @change="wareChange" clearable>
                            <el-option v-for="item in wareList" :key="item.id" :label="item.ware_name" :value="item.id" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item label="箱号" prop="box_num">
                        <el-input v-model="form.box_num" placeholder="请输入箱号" clearable></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-col :span="12">
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" type="textarea" clearable></el-input>
                </el-form-item>
            </el-col>
            <el-form-item label="采购入库单" prop="detail">
                <el-button type="primary" size="small" icon="el-icon-plus" @click="checkProduct">选择采购入库单</el-button>
                <sc-form-table ref="table" v-model="form.detail" :addTemplate="addTemplate" placeholder="暂无数据" height="400" hideAdd>
                    <el-table-column label="采购单号" align="center">
                        <template #default="scope">
                            {{scope.row.purchase_code}}
                        </template>
                    </el-table-column>
                    <el-table-column label="采购日期" align="center">
                        <template #default="scope">
                            {{scope.row.open_date}}
                        </template>
                    </el-table-column>
                    <el-table-column label="入库日期" align="center" prop="entry_date"></el-table-column>
                    <el-table-column label="数量" align="center" prop="total_number"></el-table-column>
                    <el-table-column label="总额" align="center" prop="total_money"></el-table-column>
                    <el-table-column label="备注" prop="remark" align="center">
                        <template #default="scope">
                            <el-input v-model="scope.row.remark" placeholder="备注"></el-input>
                        </template>
                    </el-table-column>
                </sc-form-table>
            </el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit(0)">保存</el-button>
			<el-button v-if="mode == 'add'" type="primary" :loading="isSaveing" @click="submit(1)">保存并审核</el-button>
		</template>
	</el-dialog>
    <select-inorder v-if="dialog.order" ref="orderDialog" @success="orderUpdate" @closed="dialog.order=false" :query="{status:1}"></select-inorder>
    
</template>

<script>
import SelectInorder from '../inhouse/selectInorder.vue';
	export default {
        components: { SelectInorder },
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看',
				},
				visible: false,
				isSaveing: false,
				form: {
					id:"",
					logistics_code: "",
					box_num: "",
					logistics_date: this.moment().format('YYYY-MM-DD'),
					remark: "",
					ware_id: "",
                    detail:[],
                    is_check: 0,
				},
                addTemplate:{},
                dialog: {
                    order: false,
                },
                wareList:[],
                ware_id: "",
			}
		},
		mounted() {
            this.getWare()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(e){
                this.form.is_check = e
                this.$refs.dialogForm.validate(async (valid) => {
                    if (valid) {
                        this.isSaveing = true;
                        var res  = [];
                        if (this.form.id) {
                            res = await this.$API.procure.internal.edit.post(this.form);
                        }else{
                            res = await this.$API.procure.internal.add.post(this.form);
                        }
                        this.isSaveing = false;
                        if(res!=''){
                            if(res.code == 1){
                                this.visible = false;
                                this.$emit('success', this.form, this.mode)
                                this.$message.success("操作成功")
                            }else{
                                this.$alert(res.message, "提示", {type: 'error'})
                            }
                        }
                    }
                })
			},
			//表单注入数据
			async setData(data){
				this.form.id = data.id
                var res = await this.$API.procure.internal.detail.get({id:data.id});
                this.form.logistics_code = res.data.logistics_code
                this.form.logistics_date = res.data.logistics_date
                this.form.ware_id = res.data.ware_id
                this.form.box_num = res.data.box_num
                this.form.remark = res.data.remark
                let array = []
                res.data.detail.map(item=>{
                    array.push({
                        id:item.id,
                        purchase_ware_id:item.purchase_ware_id,
                        purchase_code:item.purchaseware.purchase_code,
                        entry_date:item.entry_date,
                        open_date:item.open_date,
                        remark:item.remark,
                        total_money:item.total_money,
                        total_number:item.total_number,
                    })
                })
                this.form.detail = array
			},
            // 选择采购单
            checkProduct(){
                this.dialog.order = true
                this.$nextTick(() => {
                    this.$refs.orderDialog.open('add')
                })
            },
            // 选择采购单返回
            async orderUpdate(data){
                console.log('data :>> ', data);
                let array = []
                data.map(item=>{
                    array.push({
                        purchase_ware_id:item.id,
                        purchase_code:item.purchase_code,
                        entry_date:item.entry_date,
                        open_date:item.open_date,
                        remark:item.remark,
                        total_money:item.total_money,
                        total_number:item.total_number,
                    })
                })
                this.form.detail = array
            },
            // 获取仓库
            async getWare(){
                var res = await this.$API.goods.ware.all.get();
                this.wareList = res.data;
            },
            async getLocationList(row){
                const res = await this.$API.goods.ware.position.post({ware_id:row.ware_id})
                row.locationList = res.data
            },
            onFocus(event){
                event.target.select();
            },
            wareChange(e){
                this.form.detail.map(item => item.ware_id = e)
            },
            count(row){
                if(row.number > row.surplus_number){
                    this.$message.error("本次入库数量不能超过剩余可入库数量")
                    row.number = row.surplus_number
                }
            },
		}
	}
</script>

<style>
</style>
